package com.gmail.viktor.yuryev.model;

import com.intellij.openapi.components.*;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Viktor Yuryev on 4/10/2014.
 */

@State(
        name = "MyRemoteServers",
        storages = {
                @Storage(
                        file = StoragePathMacros.APP_CONFIG + "/servers.xml"
                )}
)

public class MyRemoteServers implements PersistentStateComponent<Element> {
    private List<ServerDetails> servers;


    private MyRemoteServers() {
        servers = new ArrayList<ServerDetails>();
    }

    public static MyRemoteServers getInstance() {
        MyRemoteServers service = ServiceManager.getService(MyRemoteServers.class);
        return service;
    }


    /*  public void load() {
          ServerDetails e = new ServerDetails("csapp013co.netcracker.com", "yuryev", "Shket1111", 22);

          e.addLink("C:\\SVN\\Boxster\\tools_common\\site", "/u02/netcracker/toms/u27_c13_6830/applications/NetCracker/NetCrackerWebApp");
          e.addLink("c:\\TEMP\\test_classes\\scripts\\com\\", "/netcracker/config/u27_c13_6830/applications/NetCracker/APP-INF/temp");
          e.setServerHome("/netcracker/config/u27_c13_6830");
          e.setSudoPrefix("sudo -u netcrk ");
          servers.add(e);
      }
  */
    public List<ServerDetails> getServerList() {
        return new ArrayList<ServerDetails>(servers);
    }

    public void addServer(ServerDetails connectionData) {
        servers.add(connectionData);
    }

    @Override
    public Element getState() {
        return XmlSerializer.serialize(servers.toArray(new ServerDetails[servers.size()]));
    }

    @Override
    public void loadState(Element state) {
        servers.clear();
        String name = ServerDetails.class.getSimpleName();
        for (Element element : state.getChildren(name)) {
            servers.add(XmlSerializer.deserialize(element, ServerDetails.class));
        }
    }


    public void setServers(List<ServerDetails> newRepositories) {
        servers.clear();
        servers.addAll(newRepositories);
    }
}
